package com.lb.videoplay.activity;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jude.swipbackhelper.SwipeBackHelper;
import com.lb.videoplay.R;
import com.lb.videoplay.adapter.VideoAdapter;
import com.lb.videoplay.model.VideoModel;
import com.lb.videoplay.statuslib.StatusUtil;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.lb
 * 时间： 2018/11/15
 * 包名： com.lb.videoplay.activity
 * 描述： 类型详情
 */
public class DetailsVideoActivity extends FragmentActivity implements View.OnClickListener{

    private TextView mCentent;
    private TextView mLeftIcon;
    private XBanner mXBanner;
    private ListView mRcAssort;
    private VideoAdapter mVideoAdapter;
    private List<VideoModel> mVideoModels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_video);
        initWidget();
        initView();
        initEvent();
        initBanner();
        initVideo();
    }

    //初始化
    private void initWidget() {
        mCentent = findViewById(R.id.center_title);
        mLeftIcon = findViewById(R.id.left_title_icon);
        mXBanner = findViewById(R.id.view_pagers);
        mRcAssort = findViewById(R.id.rv_featured);
    }

    //初始化标题
    private void initBanner() {
        final List<String> imgesUrl = new ArrayList<>();
        imgesUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1542803531578&di=9e1e2c22fa996190039650f5cd73986c&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F012555566a9cf86ac7256b296684e3.jpg");
        imgesUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543398285&di=740e3c000a12b2a55a776921f81cc8bd&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0160b95aa7be86a801206d963e0141.jpg%402o.jpg");
        imgesUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543398311&di=7edc8201ae62da834d0d8f705b1d61c9&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01afe159f937b3a801216a4b0b52ff.jpg%402o.jpg");

        //添加轮播图片数据（图片数据不局限于网络图片、本地资源文件、View 都可以）,刷新数据也是调用该方法
        mXBanner.setData(imgesUrl, null);//第二个参数为提示文字资源集合
        //加载广告图片
        mXBanner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                //1、此处使用的Glide加载图片，可自行替换自己项目中的图片加载框架
                //2、返回的图片路径为Object类型，你只需要强转成你传输的类型就行，切记不要胡乱强转！
                RequestOptions requestOptions = new RequestOptions()
                        .placeholder(R.drawable.iv_error)
                        .error(R.drawable.iv_error)
                        .fallback(R.drawable.iv_error);
                Glide.with(DetailsVideoActivity.this).load(imgesUrl.get(position)).apply(requestOptions).into((ImageView) view);
            }
        });
    }
    //初始化Video
    private void initVideo(){
        mVideoModels=new ArrayList<>();
        VideoModel videoModel=new VideoModel();
        videoModel.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/DC59A1833B54739022D9AD0AB0EF31B98999241F_size203_w870_h584.jpeg");
        videoModel.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3993983-102-009-160242.mp4?vid=1875a092-e4f6-4182-a23b-f5749cb2e875&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=FPS游戏站&cat=85-86&ptype=85&platform=pc&sourceType=h5&dt=1540972197000&gid=CKnh8Wd4k2f3&sign=a32434e75bb4db7da53393da97162a4b&tm=1543916185592");
        videoModel.setType(1);
        mVideoModels.add(videoModel);

        VideoModel videoMode2=new VideoModel();
        videoMode2.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/A969409A82269DDF02116B787F9892399C588624_size120_w640_h360.jpeg");
        videoMode2.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3991998-102-009-154028.mp4?vid=b2b26a2b-4874-4f9f-b8b5-6d0996c744d8&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=快手美女&cat=85-86&ptype=85&platform=pc&sourceType=h5&dt=1540971082000&gid=dErFIWd4kKQr&sign=3c4fe7b5b8ab863661044351da5b00fe&tm=1543916004397");
        videoModel.setType(2);
        mVideoModels.add(videoMode2);

        VideoModel videoMode3=new VideoModel();
        videoMode3.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/C47F52B94E09864AB3B3447AB0D3EF1055F65DD2_size110_w462_h410.jpeg");
        videoMode3.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3991569-102-009-153425.mp4?vid=d5e3d042-b40e-4783-a8fa-d9e89d68f2ab&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=快手美女&cat=85-86&ptype=85&platform=pc&sourceType=h5&dt=1540970772000&gid=Ge4wYWd4kke2&sign=fb0780ffdd50d51d450129d3e156e107&tm=1543916111800");
        videoModel.setType(3);
        mVideoModels.add(videoMode3);

        VideoModel videoMode15 = new VideoModel();
        videoMode15.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/7034E8CD519D6D659FBF73B752DA80332D7C2677_size159_w580_h326.jpeg");
        videoMode15.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3994532-102-009-161237.mp4?vid=f7ce2289-c00b-43b7-aae4-96ec978911b7&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=网游那点事&cat=172-176&ptype=172&platform=pc&sourceType=h5&dt=1540972708000&gid=cz2kIWd4nA_Y&sign=ad8b318c76c8b8029c0906930d5070cf&tm=1543916752793");
        videoModel.setType(15);
        mVideoModels.add(videoMode15);

        VideoModel videoMode16 = new VideoModel();
        videoMode16.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/589D32F1C6E11F5B57C88A5850B10DCA30D2A4EE_size147_w580_h326.jpeg");
        videoMode16.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3994565-102-009-161240.mp4?vid=ab686646-6f6d-4bd1-a855-1006ce40d1ac&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=主机游戏那点事&cat=172-176&ptype=172&platform=pc&sourceType=h5&dt=1540972740000&gid=ThDOIWd4nHGf&sign=8e6220595fd3ac94565c287003b15bc3&tm=1543916777889");
        videoModel.setType(16);
        mVideoModels.add(videoMode16);

        VideoModel videoMode13 = new VideoModel();
        videoMode13.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/6A7E3FB3C6E85D7223BE0C84C77D9B5DC68AE3E8_size104_w580_h325.jpeg");
        videoMode13.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3991641-102-009-153526.mp4?vid=86aed2b8-e5df-43eb-96db-04275ed9c67b&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=嘻哈益食坊&cat=94-96&ptype=94&platform=pc&sourceType=h5&dt=1540970830000&gid=nk4FgWd4jfho&sign=a1d5fa3609a9179905e9aeee4d6c9399&tm=1543915829355");
        videoModel.setType(13);
        mVideoModels.add(videoMode13);

        VideoModel videoMode14 = new VideoModel();
        videoMode14.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/C2515D6FCDC157D88B4A92489B7B1B0D367DEC5B_size503_w1295_h863.jpeg");
        videoMode14.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3994632-102-009-161338.mp4?vid=3d15f7fd-3e9d-4480-ab18-7db1842cb56e&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=欧阳华北&cat=172-178&ptype=172&platform=pc&sourceType=h5&dt=1540972792000&gid=sQuEQWd4msQd&sign=100355b7be8e14b8bfd92f860d3bf107&tm=1543916667935");
        videoModel.setType(14);
        mVideoModels.add(videoMode14);

        mVideoAdapter = new VideoAdapter(this,mVideoModels);

        mRcAssort.setAdapter(mVideoAdapter);
        mRcAssort.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int lastVisibleItem = firstVisibleItem + visibleItemCount;
                //大于0说明有播放
                if (GSYVideoManager.instance().getPlayPosition() >= 0) {
                    //当前播放的位置
                    int position = GSYVideoManager.instance().getPlayPosition();
                    //对应的播放列表TAG
                    if (GSYVideoManager.instance().getPlayTag().equals(VideoAdapter.TAG)
                            && (position < firstVisibleItem || position > lastVisibleItem)) {
                        if(GSYVideoManager.isFullState(DetailsVideoActivity.this)) {
                            return;
                        }
                        //如果滑出去了上面和下面就是否，和今日头条一样
                        GSYVideoManager.releaseAllVideos();
                        mVideoAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mXBanner.startAutoPlay();
        GSYVideoManager.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mXBanner.stopAutoPlay();
    }

    //点击事件
    private void initEvent() {
        mLeftIcon.setOnClickListener(this);
    }

    //初始化控件
    private void initView() {
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色
        StatusUtil.setSystemStatus(this, true, true);
        SwipeBackHelper.onCreate(this);  //右侧滑动退出
        mCentent.setText(getIntent().getStringExtra("type"));
        Drawable nav_up = ResourcesCompat.getDrawable(getResources(), R.drawable.back, null);
        nav_up.getMinimumHeight();
        nav_up.setBounds(0, 0, nav_up.getIntrinsicWidth(), nav_up.getIntrinsicWidth());
        mLeftIcon.setCompoundDrawables(null, null, nav_up, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SwipeBackHelper.onDestroy(this);
        mXBanner.stopAutoPlay();
        GSYVideoManager.releaseAllVideos();
    }

    @Override
    public void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (GSYVideoManager.backFromWindowFull(this)) {
            return;
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        SwipeBackHelper.onPostCreate(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_title_icon:
                finish();
                break;
        }
    }

}
